package com.example.demo.service.impl;
import com.example.demo.entity.File;
import com.example.demo.entity.Folder;
import com.example.demo.entity.User;
import com.example.demo.exception.file.InvalidPathException;
import com.example.demo.mapper.SearchMapper;
import com.example.demo.service.SearchService;
import com.example.demo.utils.FolderUtils;
import com.example.demo.utils.UserNameUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {
    @Resource
    SearchMapper searchMapper;
    private UserNameUtils userNameUtils=new UserNameUtils();
    @Override
    public String shareFile(String path) throws InvalidPathException {
        Folder folder=searchMapper.selectFolderByPath(UserNameUtils.getUsername()+"/"+path);
        if(folder==null){
            throw new InvalidPathException();
        }
        return FolderUtils.IP+"/"+UserNameUtils.getUsername()+"/"+path;
    }

    @Override
    public List<File> searchFile(String orderName, Boolean ascending, String name, Boolean be_star, Boolean be_share, Boolean be_new, String keyWords) throws IOException {
        //通过升降序，路径，标签，是否加星置顶分享最新删除，关键字搜索
        Subject subject= SecurityUtils.getSubject();
        User user1 =(User) subject.getPrincipal();
        String username=user1.getUsername();
        List<File> fileList =null;

        if(be_new){//最新文档搜索
            if(orderName.equals("创建时间")){
                if(ascending==true){
                    fileList = searchMapper.groupByCreateTime(keyWords, username);
                }else {
                    fileList = searchMapper.groupByCreateTime2(keyWords, username);
                }
            }else if(orderName.equals("修改时间")){
                if(ascending){
                    fileList = searchMapper.groupByUpdateTime(keyWords, username);
                }else {
                    fileList = searchMapper.groupByUpdateTime2(keyWords, username);
                }

            }else if(orderName.equals("文件名称")){
                if(ascending==true){
                    fileList = searchMapper.groupByTitle(keyWords, username);
                }else {
                    fileList = searchMapper.groupByTitle2(keyWords, username);
                }

            }else if(orderName.equals("文件大小")){
                if(ascending){
                    fileList = searchMapper.groupBySize(keyWords, username);
                }else {
                    fileList = searchMapper.groupBySize2(keyWords, username);
                }
            }
        }else if(be_share){
            if(orderName.equals("创建时间")){
                if(ascending==true){
                    fileList = searchMapper.groupByCreateTime(keyWords, username);
                }else {
                    fileList = searchMapper.groupByCreateTime2(keyWords, username);
                }
            }else if(orderName.equals("修改时间")){
                if(ascending){
                    fileList = searchMapper.groupByUpdateTime(keyWords, username);
                }else {
                    fileList = searchMapper.groupByUpdateTime2(keyWords, username);
                }

            }else if(orderName.equals("文件名称")){
                if(ascending==true){
                    fileList = searchMapper.groupByTitle(keyWords, username);
                }else {
                    fileList = searchMapper.groupByTitle2(keyWords, username);
                }

            }else if(orderName.equals("文件大小")){
                if(ascending){
                    fileList = searchMapper.groupBySize(keyWords, username);
                }else {
                    fileList = searchMapper.groupBySize2(keyWords, username);
                }
            }

        }else if(be_star){
            if(orderName.equals("创建时间")){
                if(ascending==true){
                    fileList = searchMapper.Star_CreateTime(keyWords, username);
                }else {
                    fileList = searchMapper.Star_CreateTime2(keyWords, username);
                }
            }else if(orderName.equals("修改时间")){
                if(ascending){
                    fileList = searchMapper.Star_UpdateTime(keyWords, username);
                }else {
                    fileList = searchMapper.Star_UpdateTime2(keyWords, username);
                }

            }else if(orderName.equals("文件名称")){
                if(ascending==true){
                    fileList = searchMapper.Star_Title(keyWords, username);
                }else {
                    fileList = searchMapper.Star_Title2(keyWords, username);
                }

            }else if(orderName.equals("文件大小")){
                if(ascending){
                    fileList = searchMapper.Star_Size(keyWords, username);
                }else {
                    fileList = searchMapper.Star_Size2(keyWords, username);
                }
            }
        }else if(name!=null){
            if(orderName.equals("创建时间")){
                if(ascending==true){
                    fileList = searchMapper.Sign_CreateTime(keyWords, username,name);
                }else {
                    fileList = searchMapper.Sign_CreateTime2(keyWords, username,name);
                }
            }else if(orderName.equals("修改时间")){
                if(ascending){
                    fileList = searchMapper.Sign_UpdateTime(keyWords, username,name);
                }else {
                    fileList = searchMapper.Sign_UpdateTime2(keyWords, username,name);
                }
            }else if(orderName.equals("文件名称")){
                if(ascending==true){
                    fileList = searchMapper.Sign_Title(keyWords, username,name);
                }else {
                    fileList = searchMapper.Sign_Title2(keyWords, username,name);
                }

            }else if(orderName.equals("文件大小")){
                if(ascending){
                    fileList = searchMapper.Sign_Size(keyWords, username,name);
                }else {
                    fileList = searchMapper.Sign_Size2(keyWords, username,name);
                }
            }
        }
        if(fileList==null){
            throw new NullPointerException();
        }
        userNameUtils.removeUsername(fileList,username);

        return fileList;
    }
    @Override
    public Object searchNextFile(String orderName, Boolean ascending, String filePath, String keyWords) throws IOException {
        String filepath1=filePath;
        String[] path=filepath1.split("/");
        int a=path.length+1;     //filePath被拆分后的路径长度；
        String username =userNameUtils.getUsername();
        List<Object> objectList = new ArrayList<Object>();
        List<Folder> folderList=null;
        List<File> fileList=null;
        filePath=filePath+"/";
        folderList=searchMapper.SearchFolder(username,filePath);//搜索文件夹
        userNameUtils.removeUsername2(folderList);//去掉文件夹路径的用户名
        userNameUtils.nextLevel2(folderList,a);
        if(filePath!=null){
            if(orderName.equals("创建时间")){
                if(ascending==true){
                    fileList = searchMapper.filePath_CreateTime(keyWords, username,filePath);
                }else {
                    fileList = searchMapper.filePath_CreateTime2(keyWords, username,filePath);
                }
            }else if(orderName.equals("修改时间")){
                if(ascending){
                    fileList = searchMapper.filePath_UpdateTime(keyWords, username,filePath);
                }else {
                    fileList = searchMapper.filePath_UpdateTime2(keyWords, username,filePath);
                }
            }else if(orderName.equals("文件名称")){
                if(ascending==true){
                    fileList = searchMapper.filePath_Title(keyWords, username,filePath);
                }else {
                    fileList = searchMapper.filePath_Title2(keyWords, username,filePath);
                }
            }else if(orderName.equals("文件大小")) {
                if (ascending) {
                    fileList = searchMapper.filePath_Size(keyWords, username, filePath);
                } else {
                    fileList = searchMapper.filePath_Size2(keyWords, username, filePath);
                }
            }
            userNameUtils.removeUsername(fileList,username);
            userNameUtils.nextLevel(fileList,a);
            userNameUtils.removeFile(folderList,fileList);//去除重复的文件，保留下一级文件夹
            objectList.add(folderList);
            objectList.add(fileList);
        }else{
            throw new NullPointerException();
        }
        return objectList;
    }

    @Override
    public Object searchDeletedFile(String orderName, Boolean ascending, Boolean be_deleted, String keyWords) throws IOException {
        String username =userNameUtils.getUsername();
        List<Object> objectList = new ArrayList<Object>();
        List<Folder> folderList=null;
        List<File> fileList=null;
        folderList=searchMapper.SearchDeletdeFolder(username);//搜索回收站中的文件夹
        userNameUtils.removeUsername2(folderList);//去掉文件夹路径的用户名
        userNameUtils.nextLevel2(folderList,2);
        if(be_deleted==true){
            if(orderName.equals("创建时间")){
                if(ascending==true){
                    fileList = searchMapper.Delete_CreateTime(keyWords, username);
                }else {
                    fileList = searchMapper.Delete_CreateTime2(keyWords, username);
                }
            }else if(orderName.equals("修改时间")){
                if(ascending){
                    fileList = searchMapper.Delete_UpdateTime(keyWords, username);
                }else {
                    fileList = searchMapper.Delete_UpdateTime2(keyWords, username);
                }
            }else if(orderName.equals("文件名称")){
                if(ascending==true){
                    fileList = searchMapper.Delete_Title(keyWords, username);
                }else {
                    fileList = searchMapper.Delete_Title2(keyWords, username);
                }

            }else if(orderName.equals("文件大小")){
                if(ascending){
                    fileList = searchMapper.Delete_Size(keyWords, username);
                }else {
                    fileList = searchMapper.Delete_Size2(keyWords, username);
                }
            }
            userNameUtils.removeUsername(fileList,username);
            userNameUtils.nextLevel(fileList,2);
            userNameUtils.removeFile(folderList,fileList);//去除重复的文件，保留下一级文件夹
            objectList.add(folderList);
            objectList.add(fileList);
        }else{
            throw new NullPointerException();
        }
        return objectList;
    }
}
